package org.arch_learn.servlet_container.components.connector.adapters;

import org.arch_learn.servlet_container.pojo.CustomHttpServletRequest;
import org.arch_learn.servlet_container.pojo.Request;

import java.io.IOException;

public class ServletRequestAdapter {
    public static CustomHttpServletRequest getHttpServletRequest(Request request) throws IOException {
        //用"\n\r\n"分割字符串可以过滤掉空行
        String[] arr = request.getRequestText().split("\n\r\n");
        String header = arr[0];
        //用"\\r\n"分割字符串空行也作为空串存在于返回数组里
        String startLine = header.split("\\r\n")[0];
        String body = arr.length > 1 ? arr[1] : null;

        System.out.println("startLine为：\n" + startLine);
        System.out.println("header为：\n" + header);
        System.out.println("body为：\n" + body);
        CustomHttpServletRequest customHttpServletRequest = new CustomHttpServletRequest(startLine, header, body);
        return customHttpServletRequest;
    }
}
